package com.mmd.parkingspringboot.web;
import com.mmd.parkingspringboot.model.Fund;
import com.mmd.parkingspringboot.model.Project;
import com.mmd.parkingspringboot.response.Result;
import com.mmd.parkingspringboot.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;


    /**
     * 登录
     * @param phone
     * @param password
     * @return
     */
    @GetMapping("/login")
    public Result projectLogin(String phone,String password){
        Project project = projectService.projectLogin(phone,password);
        if(project != null){
            return Result.ok().data("message",project);
        }
        else{
            return Result.error().data("message",project);
        }
    }

    /**
     * 注册
     * @param name
     * @param phone
     * @param password
     * @return
     */
    @GetMapping("/insert")
    public Result projectInsert(String name,String phone,String password,String email){
        Project project = new Project();
        project.setProjectName(name);
        project.setProjectPhone(phone);
        project.setProjectPassword(password);
        project.setProjectEmail(email);
        int a = projectService.projectInsert(project);
        if(a == 1){
            return Result.ok().data("message",a);
        }
        else{
            return Result.error().data("message",a);
        }
    }

    /**
     * 修改
     * @param id
     * @param name
     * @param phone
     * @param password
     * @param lastPhone
     * @return
     */
    @GetMapping("update")
    public Result projectUpdate(Integer id,String name,String phone,String password,String lastPhone){
//        这里的lastPhone是修改之前的手机号，作用待定
        Project project = new Project();
        project.setProjectId(id);
        project.setProjectName(name);
        project.setProjectPhone(phone);
        project.setProjectPassword(password);
        int a = projectService.projectUpdate(project);
        if(a == 1){
            return Result.ok().data("message",a);
        }
        else{
            return Result.error().data("message",a);
        }
    }

    /**
     * 显示所有项目公司
     * @return
     */
    @GetMapping("/all")
    public Result all(){
        List<Project> list = projectService.all();
        return Result.ok().data("message",list);
    }

    /**
     * 删
     * @param projectId
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer projectId){
        int a = projectService.delete(projectId);
        return Result.ok().data("message",a);
    }

    /**
     * 根据邮箱找回密码
     * @param email
     * @return
     */
    @GetMapping("/find")
    public Result projectFind(String email){
        Project project = projectService.projectFind(email);
        projectService.sendSimpleMail("1031892437@qq.com",
                email,
                email,
                "找回密码",
                project.getProjectPassword());
        return Result.ok().data("message","已发送至邮箱");
    }
}
